﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Palsta.Core
{
    public class XslAttribute : XslElement
    {

        /*
         * Inner members
         */ 
        protected XmlNode _parentHTMLNode;

        /*
         * Constructors
         */ 
        public XslAttribute(XmlNode xslNode)
            : base(xslNode)
        {
             _jsFunctionName = _functionPrefix + "attribute";
             _canReactEvent = true;
            _parentHTMLNode = GetHTMLRootNode();
        }

        /*
         * Overrides
         */
        /// <summary>
        /// Use the set the attribute of a html element
        /// This method will get the return value of all sub-elements as value and attribute of xls node as the name 
        /// e.g:
        ///function xslt_attribute_24() { 
        ///  var value = '';
        ///  value += xslt_text_25();
        ///  value += xslt_choose_26();
        ///  $('#my:field1').attr('style',value);
        ///}
        /// </summary>
        /// <returns></returns>
        public override String GetFunctionDeclareScript()
        {
            String name = _xmlNode.Attributes["name"].Value;
            String result = "function " + _jsFunctionName + "_" + _index.ToString() + "() { \r\n";
            result += "var value = '';";
            foreach (XslElement node in _xslChildNodes)
            {
                result += "\r\nvalue += " + node.GetFunctionCallingScript();
            }
            if (_parentHTMLNode != null)
            {
                result += "\r\n$('#" + _parentHTMLNode.Attributes["xd:binding"].Value + "').attr('" + name + "',value);";
            }
            result += "\r\n}\r\n";
            return result;
        }
    }
}
